package com.yc.resfoods.configs;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityException;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException;
import com.alibaba.csp.sentinel.slots.system.SystemBlockException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName MySentinelExceptionHandler
 * @since: 2023/8/26 11:55
 * @auth: kirito
 * @description:
 **/
@Component
public class MySentinelExceptionHandler implements BlockExceptionHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {
        String msg = null;
        if (e instanceof FlowException) {
            msg = "访问频繁，请稍后再试";
        } else if (e instanceof DegradeException) {
            msg = "系统降级";
        } else if (e instanceof ParamFlowException) {
            msg = "热点参数异常：" + e.getMessage() + ","
                    + ((ParamFlowException) e).getResourceName()
                    + "," + e.getRule();
        }else if (e instanceof SystemBlockException) {
            msg = "系统规则限流或降级";
        }else if (e instanceof AuthorityException) {
            msg = "授权规则不通过";
        }else {
            msg = "位置限流降级";
        }

        //http状态码
        response.setStatus(200);
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type","application/json;charset=utf-8");
        response.setContentType("application/json;charset=utf-8");
        Map<String, Object> map = new HashMap();
        map.put("code", 0);
        map.put("msg", msg);


        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(map);
        PrintWriter writer = response.getWriter();
        writer.write(json);
        writer.flush();


    }
}
